package com.vf.cloud.paas.app.agent.common.server.stun;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.common.util.CmdUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class StunServer {
	
	private int defaultPort;

	@Async
	public void run(String installPath,int port) {
		if(defaultPort!=0) {
			String Pid = CmdUtil.getPid(defaultPort);
			if (!StrKit.isBlank(Pid)) {
				CmdUtil.killByPid(Pid);
			}
		}
		defaultPort=port;
		String Pid = CmdUtil.getPid(port);
		if (!StrKit.isBlank(Pid)) {
			CmdUtil.killByPid(Pid);
		}

		log.info(">>>正在启动 --> STUNServer:" + port);
		if (!genStartBat(installPath,port)) {
			log.error(">>>生成启动Bat脚本失败 --> STUNServer:" + port);
			return;
		}
		executeCmd(String.format("%s/Relay/Start_STUNServer.bat", installPath));
	}

	private int executeCmd(String command) {
		try {
			Runtime runtime = Runtime.getRuntime();
			Process process = runtime.exec(command);
			new Thread(new ConsoleThread(new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))))
					.start();
			return process.waitFor();
		} catch (InterruptedException | IOException e) {
		}
		return -1;
	}

	class ConsoleThread implements Runnable {
		private BufferedReader bfr = null;

		public ConsoleThread(BufferedReader bfr) {
			this.bfr = bfr;
		}

		@Override
		public void run() {
			String line = null;
			try {
				while ((line = bfr.readLine()) != null) {
					log.info(line);
					if (line.contains("Listening")) {
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean genStartBat(String installPath,int port) {
		BufferedWriter out = null;
		try {
			try {
				File f = new File(String.format("%s/Relay/Start_STUNServer.bat", installPath));
				if (f.exists()) {
					f.delete();
				}
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, true)));
				out.write("@echo off \r\n");
				out.write(String.format("%s \r\n", installPath.substring(0, 2)));
				out.write(String.format("cd %s/Relay \r\n", installPath));
				out.write(String.format("stunserver.exe 0.0.0.0:%s \r\n", port));
				out.write("echo \"startStunserver1999\" \r\n");
				out.write("exit");
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
			}
		}
		return false;
	}

	public void destroy() {
		if(defaultPort!=0) {
			String Pid = CmdUtil.getPid(defaultPort);
			if (!StrKit.isBlank(Pid)) {
				CmdUtil.killByPid(Pid);
			}
		}
	}

}
